package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import edu.fim.core.util.BmiCategories;
import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.controller.exception.ControllerException;


public class ProfileForm extends AbstractForm {

	private static final long serialVersionUID = -6443523291947515953L;

	private ConsumerController consumerController;
	private JLabel nameLabel;
	private JLabel ageLabel;
	private JLabel heightLabel;
	private JLabel weightLabel;
	private JLabel caloriesThresholdLabel;

	public ProfileForm(
			String titulo,
			ConsumerController consumerController) {
		super(titulo);
		initialize();
		this.consumerController = consumerController;
		update();
	}

	public void update() {
		nameLabel.setText(consumerController.getCurrentConsumer().getName());
		ageLabel.setText(consumerController.getCurrentConsumer().getAge().toString());
		heightLabel.setText(consumerController.getCurrentConsumer().getHeight().toString());
		weightLabel.setText(consumerController.getCurrentConsumer().getWeight().toString());
		caloriesThresholdLabel.setText(consumerController.getCurrentConsumer().getCaloriesThreshold().toString());
		pack();
	}

	protected void initializeDynamicComponents() {
		nameLabel = new JLabel();
		ageLabel = new JLabel();
		heightLabel = new JLabel();
		weightLabel = new JLabel();
		caloriesThresholdLabel = new JLabel();
	}

	protected void buildLayout() {
		GroupLayout layout = new GroupLayout(getContentPane());

		Container containerDados = criarContainerDados();
		Component componenteCalculoIMC = criarComponenteCalculoIMC();
		Component componenteRetornoTela = criarComponenteRetornoTela();

		layout.setHorizontalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(containerDados)
						.addGroup(layout.createSequentialGroup()
								.addComponent(componenteCalculoIMC)
								.addComponent(componenteRetornoTela))));

		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addComponent(containerDados)
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(componenteCalculoIMC)
						.addComponent(componenteRetornoTela)));

		getContentPane().setLayout(layout);
	}

	private Container criarContainerDados() {
		Container container = new Container();
		GroupLayout layout = new GroupLayout(container);

		JLabel nome = new JLabel("Name:");
		JLabel idade = new JLabel("Age:");
		JLabel altura = new JLabel("Height:");
		JLabel peso = new JLabel("Weight:");
		JLabel limiarCalorias = new JLabel("Calories Threshold:");
		Component componenteAlteracaoLimiar = criarComponentAlteracaoLimiar();

		layout.setHorizontalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(nome)
						.addComponent(idade)
						.addComponent(altura)
						.addComponent(peso)
						.addComponent(limiarCalorias))
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
								.addComponent(nameLabel)
								.addComponent(ageLabel)
								.addComponent(heightLabel)
								.addComponent(weightLabel)
								.addComponent(caloriesThresholdLabel))
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(componenteAlteracaoLimiar)));

		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(nome)
						.addComponent(nameLabel))
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(idade)
								.addComponent(ageLabel))
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(altura)
										.addComponent(heightLabel))
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
												.addComponent(peso)
												.addComponent(weightLabel))
												.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
														.addComponent(limiarCalorias)
														.addComponent(caloriesThresholdLabel)
														.addComponent(componenteAlteracaoLimiar)));
		container.setLayout(layout);

		return container;
	}

	private Component criarComponentAlteracaoLimiar() {
		AbstractButton componenteAlteracaoLimiar = new JButton(
				"Change Threshold");
		componenteAlteracaoLimiar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteAlteracaoLimiarActionPerformed(evt);
			}
		});
		return componenteAlteracaoLimiar;
	}

	private Component criarComponenteCalculoIMC() {
		AbstractButton componenteCalculoIMC = new JButton("My BMI");
		componenteCalculoIMC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteCalculoIMCActionPerformed();
			}
		});
		return componenteCalculoIMC;
	}

	private Component criarComponenteRetornoTela() {
		AbstractButton componenteRetornoTela = new JButton("Back");
		componenteRetornoTela.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteRetornoTelaActionPerformed(evt);
			}
		});
		return componenteRetornoTela;
	}

	private void componenteAlteracaoLimiarActionPerformed(ActionEvent evt) {
		String newThresholdString = JOptionPane.showInputDialog(this,
				"Type the new calories threshold: ");
		try {
			Double newThreshold = Double.parseDouble(newThresholdString);
			try {
				consumerController.setCaloriesThreshold(newThreshold);
			}
			catch (ControllerException e) {
				e.printStackTrace();
				showMessage(e.getMessage());
			}
			showMessage("Change successful.");
			update();
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
			showMessage("Please type a valid number.");
		}
	}

	private void componenteCalculoIMCActionPerformed() {
		try {
			BmiCategories bmiCategory = consumerController.determineBmi();
			showMessage("Your BMI is: " + bmiCategory.toString());
		}
		catch (ControllerException e) {
			e.printStackTrace();
			showMessage(e.getMessage());
		}
	}

	private void componenteRetornoTelaActionPerformed(ActionEvent evt) {
		this.setVisible(false);
	}
}